package com.hjx.windowtool.bk;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.hjx.windowtool.db.Tool;
import com.hjx.windowtool.util.XiaoshuoDibaquDownloadTest;
import com.hjx.windowtool.util.MyAppender;
import com.hjx.windowtool.util.XiaoShuoTest2;
import com.hjx.windowtool.util.Xiaoshuo2ksDownloadTest2;

import lombok.extern.slf4j.Slf4j;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

@Tool("DIBAQU下载小说")
@Slf4j
public class DibaquBookDownloadApp {

	private JFrame frame;
	private JTextField txt_url;
	private JTextArea textArea;
	private JTextField txt_path;
	private JTextField txt_name;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					DibaquBookDownloadApp window = new DibaquBookDownloadApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DibaquBookDownloadApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 687, 486);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.3);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		frame.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(null);
		
		JLabel label = new JLabel("地址");
		label.setBounds(25, 10, 54, 15);
		panel.add(label);
		
		txt_url = new JTextField();
		txt_url.setBounds(89, 7, 340, 21);
		panel.add(txt_url);
		txt_url.setColumns(10);
		
		JButton button = new JButton("下载");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String url = txt_url.getText();
				String path = txt_path.getText();
				String name = txt_name.getText();
				if(url==null||path==null||name==null) {
					return;
				}
				int showConfirmDialog = JOptionPane.showConfirmDialog(frame, "是否确认下载？");
				if (showConfirmDialog!=0) {
					return;
				}
				button.setEnabled(false);
				new Thread(new Runnable() {
					public void run() {
						try {
							if (url.contains("dibaqu")) {
								XiaoshuoDibaquDownloadTest.download(path, name, url);
							}else if(url.contains("2ks")) {
								Xiaoshuo2ksDownloadTest2.download(path, name, url);
							}else {
								log.info("暂不支持的地址");
							}
						} catch (Exception e) {
							log.error("下载时发生错误",e);
						}finally {
							button.setEnabled(true);
						}
					}
				}).start();
			}
		});
		button.setBounds(440, 6, 93, 23);
		panel.add(button);
		
		JLabel label_1 = new JLabel("保存");
		label_1.setBounds(25, 35, 54, 15);
		panel.add(label_1);
		
		txt_path = new JTextField();
		txt_path.setText("E:\\book\\10");
		txt_path.setBounds(89, 32, 340, 21);
		panel.add(txt_path);
		txt_path.setColumns(10);
		
		JLabel label_2 = new JLabel("名称");
		label_2.setBounds(25, 65, 54, 15);
		panel.add(label_2);
		
		txt_name = new JTextField();
		txt_name.setText("侧测试");
		txt_name.setBounds(89, 63, 340, 21);
		panel.add(txt_name);
		txt_name.setColumns(10);
		
		JButton btnchm = new JButton("打包成CHM");
		btnchm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String path = txt_path.getText();
				String name = txt_name.getText();
				try {
					XiaoShuoTest2.packageChm(path, name);
				} catch (Exception e1) {
					log.error("打包时发生错误",e1);
				}
			}
		});
		btnchm.setBounds(440, 31, 93, 23);
		panel.add(btnchm);
		
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setRightComponent(scrollPane);
		
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		scrollPane.setViewportView(textArea);
		init();
	}
	private void init() {
		MyAppender.list.add(new MyAppender.LogCallback() {
			@Override
			public void log(String s) {
				apend(s);
			}
		});
	}

	private void apend(String s) {
		if(textArea.getLineCount()>100000) {
			textArea.setText("");
		}
		textArea.append(s);
	}
}
